home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Visual Database / Visual dBase Pro v7.0 / DATA1.CAB / Utilities / Web_Wizard / WebWizard.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-20  |  10.5 KB  |  384 lines

  1. /* Webwizard.cc  Class Library for Visual dBASE 7 Web Wizards
  2.     Author:          A.A.Katz <KSoft, inc.)
  3.                      (c) 1997 All Rights Reserverd, Borland International
  4.    Version          1.0 10-10-1997
  5.  
  6.    Includes          Class Sourcefields
  7.                      Class TextFields
  8. */
  9.  
  10.  
  11. #DEFINE QueryName 1
  12. #DEFINE FieldList 2
  13.  
  14. /////////////// Class SourceFields /////////////////////////////////////////
  15. /////////////// Attempts to open table/datamodule and read in queries
  16. /////////////// and fields  ///////////////////////////////////////////////
  17. ////////////////Param:     cSourcePath = fully qualified path to table or
  18. ////////////////         dataModule
  19.  
  20. Class SourceFields(cSourcePath) of Array
  21.  
  22.    local n
  23.  
  24.    /*Important note: Must re-instantiate each time you call this method
  25.         or array will not be empty.
  26.    */
  27.  
  28.  
  29.    this.cDataPath     = ' '
  30.    this.dMod             = ''
  31.    this.cDataType     = 'DataModule'
  32.    
  33.  
  34.    ////// Method:     GetFields ////////////////////////////////////////////
  35.    ////// Purpose:     Opens DataModule or Table and reads in queries/Fields
  36.    ////// Params:     oParent  reference to parent form
  37.    ////// cDataPath:    Disk file path to Data Module or Table
  38.  
  39.    function GetFields(cSourcePath,bIsFromReport)
  40.    
  41.       local n
  42.       private aRetValue
  43.       aRetValue         = new array()  // instantiate for return values
  44.  
  45.       this.cDataPath = trim(cSourcePath)
  46.       this.cPath         = ''
  47.       if at('\',this.cDataPath) > 0
  48.          this.cPath     = substr(this.cDataPath,1,rat('\',this.cDataPath))
  49.       endif
  50.       this.nHnd         = -1
  51.       
  52.          // if it's a data module rather than a table
  53.       if upper(right(this.cDataPath,4)) = '.DMD' or bIsFromReport  
  54.  
  55.          this.cDataType = 'DataModule'
  56.  
  57.  
  58.          // attempt to open .dmd file readonly with low-level file IO
  59.  
  60.          this.nHnd = fOpen(this.cDataPath,'R')
  61.  
  62.          if this.nHnd < 0                                 // if file fails to open
  63.             msgbox('Unable to open file: '+this.oParent.TableField.value)
  64.             fClose(this.nHnd)
  65.             return aRetValue                             // go back/failed
  66.          endif
  67.  
  68.  
  69.          // Parse out Class name for data module and attempt to open it
  70.  
  71.          do
  72.  
  73.             cStr = trim(fGets(this.nHnd))
  74.  
  75.             if at('CLASS ',upper(cStr)) > 0 and at('OF',upper(cStr)) > 0
  76.  
  77.                                                      // get class name
  78.                cStr = ltrim(substr(cStr,at('CLASS',upper(cStr))+5))           
  79.                cName = trim(substr(cStr,1,at(' ',cStr)-1))
  80.                                                 // build macro string
  81.                
  82.                try
  83.                 
  84.                                     // attemp to instantiate data module       
  85.  
  86.                   set proc to (this.cDataPath) addi
  87.            
  88.                catch ( Exception e )
  89.  
  90.                   msgbox('Unable to load datamodule: '+this.cDataPath+;
  91.                       chr(13)+e.message)   
  92.                   return aRetValue
  93.  
  94.                endtry
  95.  
  96.  
  97.                // try to change directory before opening Data Module
  98.               
  99.  
  100.                cCurrPath = set('DIRECTORY')
  101.  
  102.                if len(trim(this.cPath)) > 0
  103.  
  104.                   try
  105.                      cPath = this.cPath
  106.                      cd (cPath)
  107.                
  108.                   catch (Exception e)
  109.  
  110.                      msgbox('Unable to access directory: '+cPath+;
  111.                              chr(13)+e.message)
  112.  
  113.                      cPath = cCurrPath   // restore directory
  114.                      cd (cPath)
  115.                      return aRetValue  // return empty array
  116.  
  117.                   endtry
  118.  
  119.                endif
  120.  
  121.                // Instantiate data module
  122.  
  123.                try
  124.           
  125.                   this.dMod = new &cName.()
  126.  
  127.                catch ( Exception e )
  128.  
  129.                   msgbox('Unable to create new instance of class: '+cName+;
  130.                           chr(13) + e.message+;
  131.                           chr(13) + 'File: '+e.filename+;
  132.                           chr(13) + 'Line: '+str(e.lineno,5))
  133.  
  134.                   fClose(this.nHnd)
  135.                   cPath = cCurrPath
  136.                   cd (cPath)
  137.                   return aRetValue
  138.  
  139.                endtry
  140.  
  141.                // return to default path
  142.                cPath = cCurrPath
  143.                cd (cPath)
  144.  
  145.                exit
  146.          
  147.             endif
  148.  
  149.  
  150.             Until fEof(this.nHnd)
  151.  
  152.  
  153.  
  154.          // parse out the query classes in this data module
  155.  
  156.          do
  157.  
  158.                           
  159.             cStr = ltrim(trim(fGets(this.nHnd)))        // read in one line
  160.  
  161.             if at('NEW QUERY()',upper(cStr)) > 0  // see if it's a query declaration
  162.                              
  163.                                                 // parse out query declaration
  164.                cName = substr(cStr,1,at(' ',cStr)-1)
  165.  
  166.                if at('THIS.',upper(cName)) > 0       // if "this." in line, remove it
  167.                   cName = substr(cName,at('THIS.',upper(cName))+5)
  168.                endif
  169.               
  170.                if len(trim(cName)) > 0          // add query name to array
  171.                   
  172.                   this.resize(alen(this,1)+1,2)
  173.                   n= alen(this,1)
  174.                   this[n,QueryName] = cName
  175.  
  176.                endif
  177.         
  178.             endif
  179.  
  180.          Until feof(this.nHnd)
  181.  
  182.          fClose(this.nHnd)
  183.  
  184.             // traverse rowsets and store fieldnames in this array
  185.          for n = 1 to alen(this,1)         
  186.             class::getFieldNames(n)
  187.          next
  188.  
  189.             // set return value = a single-dim array with "aliases"
  190.          aRetValue = class::GetAvailableFieldsArray()
  191.  
  192.       else                                      // if it's a table
  193.  
  194.          this.cDataType = 'Table'
  195.  
  196.          cName = substr(this.cDataPath,1, rat('.',this.cDataPath)-1)
  197.  
  198.          if at('\',cName) > 0
  199.             cName = substr(cName,rat('\',this.cDataPath)+1)
  200.             // attempt table open
  201.          endif
  202.         
  203.          // Change directory before opening query
  204.  
  205.          cCurrPath = set('DIRECTORY')
  206.       
  207.  
  208.          if len(trim(this.cPath)) > 0
  209.             Try
  210.  
  211.                cPath = this.cPath
  212.                cd (cPath)
  213.  
  214.             Catch (Exception e)
  215.                msgbox('Unable to access directory: '+cPath+;
  216.                       chr(13)+e.message)
  217.                return aRetValue   // return empty array
  218.  
  219.             EndTry
  220.  
  221.          endif
  222.      
  223.   
  224.  
  225.          // Try to open table in query
  226.          try
  227.  
  228.             this.q = new Query()
  229.             this.q.SQL = 'select * from "'+this.cDataPath+'"'
  230.             this.q.active = true
  231.  
  232.          catch ( Exception e )
  233.             // check exception and give detailed error message
  234.           
  235.             msgbox('Unable to open table: '+this.cDataPath+;
  236.                     chr(13)+e.message)
  237.  
  238.  
  239.             // go back / failed
  240.             if this.nHnd > 0
  241.                fClose(this.nHnd)
  242.             endif
  243.  
  244.             cPath = cCurrPath
  245.             cd (cPath)
  246.  
  247.             return aRetValue
  248.          endtry
  249.  
  250.          cPath = cCurrPath
  251.          cd (cPath)
  252.                                             // remove file extension
  253.       
  254.             // add element to array for this table and store table name
  255.  
  256.  
  257.          this.resize(alen(this,1)+1,2)     
  258.          n = alen(this,1)
  259.          this[n,QueryName] = cName
  260.  
  261.          Class::GetFieldNames(n)
  262.          aRetValue = class::GetAvailableFieldsArray()
  263.  
  264.  
  265.      endif
  266.  
  267.      return aRetValue   // return Available Fields array
  268.  
  269.  
  270.      /////Method: GetFieldNames ////////////////////////////////////////
  271.      /////Purpose:    Parses the fields[] array of a query object for 
  272.      /////                field names
  273.      /////                Stores fieldnames in the FieldList [n,2] element
  274.      /////                of the main array class
  275.      /////Param:        n - the current array element of the main array
  276.      
  277.      function GetFieldNames(n)
  278.         local n2
  279.         private cQueryName,cLen,nLen
  280.  
  281.                 // Get length of fields array in rowset
  282.  
  283.         if this.cDataType = 'DataModule'
  284.            cQueryName = "this.dMod."+this[n,QueryName]
  285.         else
  286.            cQueryName = "this.q"
  287.         endif
  288.         
  289.         cLen       = cQueryName+'.rowset.fields.size'
  290.         nLen       = &cLen
  291.  
  292.         // create fieldname array as element of main array
  293.  
  294.         this[n,FieldList] = New Array()
  295.         
  296.  
  297.         // populate array with fieldnames
  298.  
  299.         for n2= 1 to nLen
  300.                            // set up macro for fields[n].fieldname
  301.             cFieldMacro = cQueryName+".rowset.fields["+;
  302.                ltrim(trim(str(n2,4)))+"].fieldname"
  303.             cFieldName = &cFieldMacro.           // expand macro
  304.             
  305.             this[n,Fieldlist].Add(cFieldName)// add element/fieldname to array      
  306.  
  307.         next
  308.  
  309.  
  310.         /////Method: GetAvailableFieldsArray()
  311.         /////Purpose: Traverses This.array and makes a new array in
  312.         /////         single dimension
  313.         function GetAvailableFieldsArray
  314.  
  315.         local n
  316.         a = new array()   // create array to pass back to form
  317.                           // loop through main array
  318.         for n = 1 to alen(this,1)
  319.  
  320.            cQuery = this[n,QueryName]           // for each query in main array
  321.                                                       // enumerate the fields array
  322.  
  323.            for n2 = 1 to alen(this[n,FieldList],1)
  324.               a.add(cQuery+'->'+this[n,FieldList][n2])
  325.            next
  326.         next
  327.  
  328.         return a  // return the resultant array
  329.  
  330.  
  331.  
  332. endclass  /// end of class
  333.  
  334.  
  335.  
  336. /*
  337.   Class TextFields - gets streamsource text items from report file
  338.   Params:             - cRepPath   Path to Report file
  339.  
  340. */
  341.  
  342. Class TextFields of Array
  343.  
  344.  
  345.     ////////////////Method GetTextFields//////////////////////////////////////
  346.     ////////////////Purpose: Gets text fields from report file ///////////////
  347.    ////////////////Params: None /////////////////////////////////////////////
  348.  
  349.    function GetTextFields(cRepPath)
  350.    local nHnd
  351.  
  352.  
  353.  
  354.    nHnd = fOpen(cRepPath)
  355.    if nHnd < 0  // if you cannot open report file
  356.       msgbox('Cannot open '+cRepPath)
  357.       return this  // return empty array
  358.    endif
  359.  
  360.    do
  361.  
  362.                                            // read in the next line
  363.       cStr = upper(fGets(nHnd)) 
  364.  
  365.                                            // if it's a fields text object
  366.       if at('{||', cStr) > 0 .and. at('FIELDS',cStr) > 0
  367.                                  // add fieldname to this array
  368.  
  369.          this.add(cStr.SubString(at('FIELDS["',cStr)+7,;
  370.              at('"]',cStr)-1))
  371.  
  372.       endif
  373.            
  374.  
  375.    until fEof(nHnd)
  376.  
  377.    fClose(nHnd)
  378.  
  379.  
  380.    return this
  381.  
  382. Endclass
  383.  
  384.